/* $Id: WidgetFactory.java 52 2010-01-11 01:42:30Z aaron.day $ */
package org.crazydays.gameplan.widget;


import java.util.Properties;
import java.util.Set;
import java.util.TreeSet;

import org.crazydays.gameplan.db.Database;

/**
 * WidgetFactory
 */
public class WidgetFactory
{
    /**
     * Build widget factory from configuration.
     * 
     * @param configuration Configuration
     * @return WidgetFactory
     */
    public static WidgetFactory build(WidgetConfiguration configuration)
    {
        return new WidgetFactory(configuration);
    }

    /** widget categories */
    protected Set<WidgetCategory> categories = new TreeSet<WidgetCategory>();

    /** widget definitions */
    protected Set<WidgetDefinition> definitions =
        new TreeSet<WidgetDefinition>();

    /**
     * WidgetFactory constructor.
     * 
     * @param configuration Configuration
     */
    protected WidgetFactory(WidgetConfiguration configuration)
    {
        loadCategories(configuration);
        loadDefinitions(configuration);
    }

    /**
     * Load widget categories.
     * 
     * @param configuration Configuration
     */
    protected final void loadCategories(WidgetConfiguration configuration)
    {
        if (configuration == null) {
            throw new IllegalArgumentException("configuration == null");
        }

        for (Properties properties : configuration.getProperties()) {
            loadCategories(properties);
        }

        for (Database database : configuration.getDatabases()) {
            loadCategories(database);
        }
    }

    /**
     * Load categories from properties.
     * 
     * @param properties Properties
     */
    protected final void loadCategories(Properties properties)
    {
        if (properties == null) {
            throw new IllegalArgumentException("properties == null");
        }

        // TODO: look up categories property and load categories
    }

    /**
     * Load categories from database.
     * 
     * @param database Database
     */
    protected final void loadCategories(Database database)
    {
        if (database == null) {
            throw new IllegalArgumentException("database == null");
        }

        // TODO: load categories from database
    }

    /**
     * Load widget definitions.
     * 
     * @param configuration Configuration
     */
    protected final void loadDefinitions(WidgetConfiguration configuration)
    {
        if (configuration == null) {
            throw new IllegalArgumentException("configuration == null");
        }

        for (Properties properties : configuration.getProperties()) {
            loadDefinitions(properties);
        }

        for (Database database : configuration.getDatabases()) {
            loadDefinitions(database);
        }
    }

    /**
     * Load definitions from properties.
     * 
     * @param properties Properties
     */
    protected final void loadDefinitions(Properties properties)
    {
        if (properties == null) {
            throw new IllegalArgumentException("properties == null");
        }

        // TODO: look up categories property and load categories
    }

    /**
     * Load definitions from database.
     * 
     * @param database Database
     */
    protected final void loadDefinitions(Database database)
    {
        if (database == null) {
            throw new IllegalArgumentException("database == null");
        }

        // TODO: load definitions from database
    }

    /**
     * Get widget categories.
     * 
     * @return WidgetCategories
     */
    public Set<WidgetCategory> getCategories()
    {
        return new TreeSet<WidgetCategory>(categories);
    }

    /**
     * Get widget definitions.
     * 
     * @return Definitions
     */
    public Set<WidgetDefinition> getDefinitions()
    {
        return new TreeSet<WidgetDefinition>(definitions);
    }
}
